/**
 * @(#) CongMoneyAccountMediator.as
 *
 * @date		2009. 03. 19.
 * @version		1.0
 * @author		Park, Juneho
 * @description	경조사 신청 - Mediator
 * 				경조사를 신청하는 Presentation Logic을 관리
 */
package com.skt.common_hr.congrest.view
{
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.congrest.CelebrationFacade;
	import com.skt.common_hr.congrest.events.AccountEvent;
	import com.skt.common_hr.congrest.model.AccountProxy;
	import com.skt.common_hr.congrest.model.CongMoneyAccountProxy;
	import com.skt.common_hr.congrest.view.components.CongMoneyAccountControl;

	import flash.events.Event;

	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;

	import org.puremvc.as3.multicore.interfaces.INotification;

	public class CongMoneyAccountMediator extends CommonMediator
	{
		public static const NAME:String = "CongMoneyAccountMediator";

		public var baseDate:Date;
		public var pernr:String;

		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function CongMoneyAccountMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );

        }

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void
 		{
 			// 각 버튼들의 Event Handler를 설정하고
 			view.addEventListener(CongMoneyAccountControl.CANCEL_ACCOUNT_CONTROL, onCancel);

 			//var proxyBankCode:CongMoneyAccountProxy = facade.retrieveProxy(CongMoneyAccountProxy.NAME) as CongMoneyAccountProxy;
 			//proxyBankCode.loadBankList();
 			accountDataSelect();


 			view.addEventListener(AccountEvent.Account, onAccountSave);
 		}

 		private function accountDataSelect():void{
 			var proxyData:CongMoneyAccountProxy = facade.retrieveProxy(CongMoneyAccountProxy.NAME) as CongMoneyAccountProxy;
 			proxyData.load(pernr);
 		}

 		public function execute(pernr:String):void
 		{
 			this.pernr = pernr;
 			loadBankList();
 			accountDataSelect();
 		}

 		public function loadBankList():void
 		{
 			var proxy:AccountProxy = facade.retrieveProxy(AccountProxy.NAME) as AccountProxy;
 			if(proxy.AccountBank == null) {
 				proxy.loadBankList();
 			}
 			else {
 				view.bankComboArr = proxy.AccountBank;
 			}
 		}

 		/***
 		 * 계좌관리 팝업 호출
 		 * **/
 		private function onOpenAccountControl(event:Event):void{
 			var form:IFlexDisplayObject = PopUpManager.createPopUp(view, CongMoneyAccountControl, true);
 			PopUpManager.centerPopUp(form);
 			(form as CongMoneyAccountControl).setFocus();

 			/* var reqMediator:ReqWelfareMediator = new ReqWelfareMediator(form)
 			facade.registerMediator(reqMediator); */
 		}

 		private function onCancel(event:Event):void
 		{
 			PopUpManager.removePopUp(view);
 			trace("onCancel suss");
 		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return [
					CelebrationFacade.LOAD_BANKCODE_COMPLETE,
					CelebrationFacade.LOAD_BANKCODE_FAILED,
					CelebrationFacade.LOAD_ACCOUNTLIST_COMPLETE,
					CelebrationFacade.LOAD_ACCOUNTLIST_FAILED,
					CelebrationFacade.LOAD_ACCOUNT_SAVE_COMPLETE,
					CelebrationFacade.LOAD_ACCOUNT_SAVE_FAILED,
					CelebrationFacade.LOAD_CONGMONEY_ACCOUNT_INITIAL
					];
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName())
			{
				case CelebrationFacade.LOAD_BANKCODE_COMPLETE:
					view.bankComboArr = (note.getBody() as AccountProxy).AccountBank;
					break;
				case CelebrationFacade.LOAD_BANKCODE_FAILED:
					trace(" LOAD_BANKCODE_FAILED ");
					break;
				case CelebrationFacade.LOAD_ACCOUNTLIST_COMPLETE:
					view.userAccountData = (note.getBody() as CongMoneyAccountProxy).userAccountData;
					break;

				case CelebrationFacade.LOAD_ACCOUNT_SAVE_COMPLETE:
					PopUpManager.removePopUp(view);
					//계좌번호를 다시 로드한다.
					facade.sendNotification(CelebrationFacade.MODIFIED_ACCOUNT, this);
					break;
				case CelebrationFacade.LOAD_ACCOUNT_SAVE_FAILED:
					var proxy:CongMoneyAccountProxy = CongMoneyAccountProxy(note.getBody());
					if(proxy.retCode == 4) {
						CommonAlert.showMessage(commonFacade.bundle, "ALERT_SAME_ACCOUNT");
						PopUpManager.removePopUp(view);
					}
					else {
						trace("LoadAccountSaveFailed: " + proxy.faultMessage);
						CommonAlert.showMessage(commonFacade.bundle, "ALERT_FAILED_SAVE");
					}
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		public var BANKL:String;
		public var BANKA:String;
		public var BANKN:String;
		public var EMFTX:String;
		private function onAccountSave(event:AccountEvent):void
		{
			var proxyBankCode:CongMoneyAccountProxy = facade.retrieveProxy(CongMoneyAccountProxy.NAME) as CongMoneyAccountProxy;
 			proxyBankCode.save(this.pernr,event.accvo);
 			BANKL = event.accvo.BANKL;
 			BANKA = event.accvo.BANKA;
 			BANKN = event.accvo.BANKN;
 			EMFTX = event.accvo.EMFTX;
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():CongMoneyAccountControl
        {
            return viewComponent as CongMoneyAccountControl;
        }
	}
}